/**
 * @file 后台接口文件
 *
 * @author fanxingguang
 * 2016-10-31
 */
'use strict';

let express = require('express');
var router = express.Router();
let fs = require('fs');
let path = require('path');

let dir = path.resolve(__dirname, 'apis');
let apiFiles = fs.readdirSync(dir);
apiFiles.forEach(function (filename) {
    // 只处理js文件
    if (/\.js$/.test(filename)) {
        let pureFileName = filename.match(/(.*)?\.js?/)[1];
        let apiRouter = require('./apis/' + filename);
        for (let funcName in apiRouter.get) {
            if (apiRouter.get.hasOwnProperty(funcName)) {
                router.get('/' + pureFileName + '/' + funcName, apiRouter.get[funcName]);
                router.get('/api/' + funcName, apiRouter.get[funcName]);
            }
        }
        for (let funcName in apiRouter.post) {
            if (apiRouter.post.hasOwnProperty(funcName)) {
                router.post('/' + pureFileName + '/' + funcName, apiRouter.post[funcName]);
                router.post('/api/' + funcName, apiRouter.post[funcName]);
            }
        }
    }
});
module.exports = router;
